import datetime
from django.contrib.contenttypes.models import ContentType
from django.utils import timezone
from django.db.models import Sum
from .models import ReadNum, ReadDetail
from blog.models import Blog

def read_statistics_once_read(request, obj):
    ct = ContentType.objects.get_for_model(obj)
    key = '%s_%s_readed'%(ct.model, obj.pk)
    if not request.COOKIES.get(key):  # 没读到该键的值，则为false，加not表示true，即没读到cookie，是第一次request请求
        # 指定博客的总阅读数+1
        readnum, created = ReadNum.objects.get_or_create(content_type=ct, object_id=obj.pk)
        readnum.read_num += 1
        readnum.save()  # 数据库中该模型对象要进行保存结果

        # 指定博客的当天阅读数+1
        date = timezone.now().date()  # timezone.now()获取的是当前的时间(包含日期和具体时分)，使用timezone.now().date()可以只获取日期部分
        readDetail, created = ReadDetail.objects.get_or_create(date=date, content_type=ct, object_id=obj.pk)
        readDetail.read_num += 1
        readDetail.save()
    return key

def get_seven_days_read_data(content_type):
    today = timezone.now().date()
    dates = []
    read_nums = []
    for i in range(7,0,-1):
        date = today - datetime.timedelta(days=i)  # 排列顺序是7天前，6天前...昨天，共7天
        dates.append(date.strftime('%m/%d'))  # 将日期类型转为字符串形式
        read_details = ReadDetail.objects.filter(date=date, content_type=content_type) # 这里使用filter因为一个日期对应着多个阅读数量对象
        result = read_details.aggregate(read_num_sum=Sum('read_num'))  # 对筛选到的同一个日期的所有阅读数量对象进行聚合，得到的是一个字典的形式，key设置为read_num_sum
        read_nums.append(result['read_num_sum'] or 0)  # result是个字典，result[‘read_num_sum’]可以取到对应的当天阅读数量之和
    return dates, read_nums

def get_today_hot_data(content_type_class):
    today = timezone.now().date()
    # 通过order_by对获得当天的所有read_details对象进行降序排列，即阅读量从高到低进行排序
    today_hot_data = content_type_class.objects.filter(read_details__date=today)\
                        .values('id', 'title')\
                        .annotate(read_num_sum = Sum('read_details__read_num'))\
                        .order_by('-read_num_sum')
    return today_hot_data[:7]

def get_yesterday_hot_data(content_type_class):
    today = timezone.now().date()
    yesterday = today - datetime.timedelta(days=1)
    # 通过order_by对获得当天的所有read_details对象进行降序排列，即阅读量从高到低进行排序
    yesterday_hot_data = content_type_class.objects.filter(read_details__date=yesterday) \
                        .values('id', 'title') \
                        .annotate(read_num_sum=Sum('read_details__read_num')) \
                        .order_by('-read_num_sum')
    return yesterday_hot_data[:7]

def get_seven_days_hot_data(content_type_class):
    today = timezone.now().date()
    before_seven_date = today - datetime.timedelta(days=7)
    seven_days_hot_data = content_type_class.objects.filter(read_details__date__lt=today, read_details__date__gte=before_seven_date)\
                        .values('id', 'title')\
                        .annotate(read_num_sum = Sum('read_details__read_num'))\
                        .order_by('-read_num_sum')
    return seven_days_hot_data[:7]
